<?php

namespace App\Admin\Controllers;

use App\Admin\Traits\tabMenu;
use App\Models\Client;
use App\Models\ClientRecords;
use App\Models\Department;
use App\Models\User;
use Carbon\Carbon;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Widgets\Table;
use Illuminate\Support\Facades\Request;

class RecordsController extends BaseController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '跟进记录';

    public function handle(Request $request)
    {

        return $this->next();
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid($client_id=0)
    {       // 进入这个GRID 之后， 这样直接返回
        $gridForNew = new Grid(new ClientRecords());
        $users = User::Users();
        $users[1] = '';

        if(Admin::user()->is_leader == 1){
            // 获取当前部门下所有人
            $employee_ids = Department::getEmployees(self::current_id());
            $gridForNew->model()->whereIn('employee_id',$employee_ids);
        }else{
            $gridForNew->model()->where('employee_id', self::current_id());
        }

        if($client_id>0){
            $gridForNew->model()->where('client_id',$client_id);
        }
        $gridForNew->actions(function (Grid\Displayers\Actions $actions){
            $actions->disableView();
            $actions->disableEdit();
        });
        $gridForNew->disableRowSelector();
        $gridForNew->disableCreateButton();
        $gridForNew->disableExport();
        $gridForNew->disableFilter();
        $gridForNew->disableColumnSelector();
        $gridForNew->model()->orderBy('created_at', 'desc');
        $gridForNew->column('client_id','客户姓名')->using(Client::getAllClients());
        $gridForNew->column('employee_id','销售')->using($users);
        $gridForNew->column('records','跟进记录');
        $gridForNew->column('plan','跟进计划');
//        $gridForNew->column('record_at','计划时间');
        $gridForNew->column('created_at', '跟进时间');
        return $gridForNew;
    }

    public function show($client_id, Content $content)
    {
        $client_name = Client::getAllClients()[$client_id];
        $content
            ->title($this->title().' - '.$client_name)
            ->description($this->description['show'] ?? trans('admin.show'))
            ->row(function ($row) use ($client_id){
                $row->column(12, tabMenu::tabMenuList($client_id));
            })
            ->row(function ($row) use ($client_id){
                $row->column(6, $this->grid($client_id));
                $row->column(6, $this->form($client_id));
            });
        return $content;
    }


    public function edit($client_id, Content $content)
    {
        $client_name = Client::getAllClients()[$client_id];
        return $content
            ->title($this->title().' - '.$client_name)
            ->description($this->description['edit'] ?? trans('admin.edit'))
            ->row(function ($row) use ($client_id){
                $row->column(12, tabMenu::tabMenuList($client_id));
            })
            ->row(function ($row) use ($client_id){
                $row->column(6, $this->grid($client_id));
                $row->column(6, $this->form($client_id));
            });
    }
    /**
     * Make a show builder.
     *
     * @param mixed   $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(ClientRecords::findOrFail($id));

        $show->field('id', __('ID'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form($client_id = 0)
    {
        $form = new Form(new ClientRecords);

        $form->display('id', __('ID'));
        $form->hidden('client_id','客户ID')->default($client_id);
        $form->hidden('employee_id','员工ID')->default(self::current_id());
        $form->textarea('records','跟进记录')->required();
        $form->textarea('plan','操作计划');
        $form->datetime('record_at','计划时间');
        $form->hidden('client.last_updated_at','上次联系时间')->value(Carbon::now()->toDateTimeString());
        $form->display('created_at', __('Created At'));
        $form->display('updated_at', __('Updated At'));

        $form->tools(function (Form\Tools $tools) {
            $tools->disableDelete();
            $tools->disableView();
            $tools->disableList();
        });
        $form->disableEditingCheck();
        $form->disableCreatingCheck();
        $form->disableViewCheck();
        $form->saved(function (Form $form){
            Client::query()->where('id',$form->client_id)->update(['last_updated_at'=>Carbon::now()->toDateTimeString()]);
            if($form->isEditing()){
                return back();
            }
        });

        return $form;
    }

    public function store2(Content $content)
    {

        $form = new Form(new ClientRecords);

        $form->display('id', __('ID'));
        $form->hidden('client_id','客户ID');
        $form->hidden('employee_id','员工ID')->default(self::current_id());
        $form->textarea('records','跟进记录')->required();
        $form->textarea('plan','操作计划');
//        $form->datetime('record_at','计划时间');
//        $form->hidden('client.last_updated_at','上次联系时间')->value(Carbon::now()->toDateTimeString());
        $form->display('created_at', __('Created At'));
        $form->display('updated_at', __('Updated At'));

        $form->tools(function (Form\Tools $tools) {
            $tools->disableDelete();
            $tools->disableView();
            $tools->disableList();
        });
        $form->disableEditingCheck();
        $form->disableCreatingCheck();
        $form->disableViewCheck();
        $form->saved(function (Form $form){
            Client::query()->where('id',$form->client_id)->update(['last_updated_at'=>Carbon::now()->toDateTimeString()]);
            return redirect(back()->getTargetUrl());
        });

        return $form->store();
    }

}
